<template>
<div>
  <table>
    <tr>
      <td>商品</td>
      <td>价格</td>
      <td>操作</td>
    </tr>
    <tr v-for="i in goodslist">
      <td>{{i.name}}</td>
      <td>{{i.price}}</td>
      <td>
        <button @click="buy(i.id)">购买</button>
      </td>
    </tr>
  </table>
</div>
</template>

<script>
export default {
  name: "cgoods",
  data(){
    return{
      goodslist:[],
      userid:localStorage.getItem('userid')
    }
  },
  methods:{
    get_goods(){
      this.$axios.get('/goods/goods')
      .then(res=>{
        console.log(res.data)
        this.goodslist = res.data.list
      })
    },
    buy(id){
      if (this.userid){
        this.$axios.post('',{'userid':this.userid,'goodsid':id})
          .then(res=>{

        })
      }
      else {
        this.$router.push({path:'clogin'})
      }
    }
  },
  mounted() {
    this.get_goods()
  }

}
</script>

<style scoped>

</style>
